#!/bin/bash

cd ~/ane_service

# 清除不需要的 images 和container
./dev_tools/lst.sh

echo ""
echo "DELETE all container whose status=exited"
echo ""

# 删除所有状态为Exited或Created的container
docker rm $(docker ps -a -q -f status=exited) 1>/dev/null 2>&1
docker rm $(docker ps -a -q -f status=created) 1>/dev/null 2>&1
echo "   ID          Image       Command                Status          Names  "
sudo docker ps --format "{{.ID}}   {{.Image}}   {{.Command}}   {{.Status}}   {{.Names}}"
echo ""
echo ""

                                         
#  all images
docker_images=$(sudo docker images -a)


echo ""
echo "DELETE all images whose 'TAG' == <none>"
echo ""
 
while read -r line; do
    tag=$(echo $line | awk '{print $2}')
    if [ "-$tag" == "-<none>" ]; then
        image_id=$(echo $line | awk '{print $3}')
        echo "docker rmi $image_id"
	   sudo docker rmi $image_id        
    fi
done <<< "$docker_images"

sudo docker images -a
